package com.dongdongshop.service.impl.goods;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.goods_desc.TbGoodsDesc;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.seller.TbSeller;
import com.dongdongshop.service.goods.GoodsService;
import com.dongdongshop.service.seller.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

@Service
@org.springframework.stereotype.Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;
    @Override
    @Transactional
    public int insertGoods(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,List<TbItem> tbItems,TbSeller tbSeller) {
        int i = tbGoodsMapper.insertGoods(tbGoods);
        tbGoodsDesc.setGoodsId(tbGoods.getId());
        int i2 = tbGoodsDescMapper.insertSelective(tbGoodsDesc);
        List<TbItem> list = setListItems(tbGoods, tbGoodsDesc, tbItems);
        for (TbItem tbItem : list) {
            tbItem.setSellerId(tbGoods.getSellerId());
            tbItem.setSeller(tbSeller.getName());
            tbItemMapper.insertSelective(tbItem);
        }
        return i2;
    }
    private List<TbItem> setListItems(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc,List<TbItem> tbItems){
        List<TbItem> list = new ArrayList<>();
        for (TbItem tbItem : tbItems) {
            String spec = tbItem.getSpec();
            JSONObject object = JSON.parseObject(spec);
            Map<String, Object> innerMap = object.getInnerMap();
            Set<Map.Entry<String, Object>> entrys = innerMap.entrySet();  //此行可省略，直接将map.entrySet()写在for-each循环的条件中
            String title = tbGoods.getGoodsName();
            for(Map.Entry<String, Object> entry:entrys){
                title+=entry.getKey()+entry.getValue();
            }
            tbItem.setTitle(title);
            tbItem.setGoodsId(tbGoods.getId());
            tbItem.setStockCount(tbItem.getNum());
            tbItem.setCategoryid(tbGoods.getCategory3Id());
            tbItem.setCreateTime(new Date());
            tbItem.setUpdateTime(new Date());
            list.add(tbItem);

        }
        return list;
    }
    @Override
    public List<TbGoods> listGoodsBySellerId(TbSeller tbSeller) {
        List<TbGoods> tbGoodsList = tbGoodsMapper.listGoodsBySellerId(tbSeller);
        return tbGoodsList;
    }

    @Override
    public int updateAudit(Long[] ids) {
        return tbGoodsMapper.updateAudit(ids);
    }

    @Override
    public List<TbGoods> listGoodsByUnreviewed() {
        return tbGoodsMapper.listGoodsByUnreviewed();
    }

    @Override
    public int updateAuditToPass(Long[] ids) {
        return tbGoodsMapper.updateAuditToPass(ids);
    }

    @Override
    public TbGoods getGoodsById(Long goodsId) {
        return tbGoodsMapper.selectByPrimaryKey(goodsId);
    }
}
